<?php
include_once("class-courseattendence.php");
class Student{
    var $studentId;
    var $studentName;
    var $studentAddress;
    var $studentMobile;
    var $studentEmail;
	
    var $studentClassID;
    var $courseAttendence;
    var $courseRegistrationID;

    function Student(){
    }
	
	function setAll($sId,$sName,$sAddress,$sMobile,$sEmail){
        $this->studentId=$sId;
        $this->studentName=$sName;
		$this->studentAddress=$sAddress;
		$this->studentMobile=$sMobile;
		$this->studentEmail=$sEmail;
    }
	
    function getStudentId(){
        return $this->studentId;
    }
    function setStudentId($newSID){
        $this->studentId = $newSID;
    }  
	function getStudentName(){
        return $this->studentName;
    }
	function getStudentClassID(){
        return $this->studentClassID;
    }
	
	function checkLogin($studentName,$studentPass){ 
		$sql = "SELECT * FROM student WHERE student_id like '".$studentName."' and student_password like '".$studentPass."'";
		$query = mysql_query($sql);  
		$check = false;
		while($row=mysql_fetch_array($query)) {  
			$check = true;
			$this->setAll($row["STUDENT_ID"],$row["STUDENT_NAME"],$row["STUDENT_ADDRESS"],$row["STUDENT_MOBILE"],$row["STUDENT_EMAIL"]);
			$this->studentClassID = $row["CLASS_ID"];
			break;
		}
		return $check;
	}
	
	function getAttendance() {
		if(!isset($this->courseAttendence)){
			$this->courseAttendence = array();
			$sql = "SELECT * FROM `courseattendence` WHERE student_id like '". $this->getStudentId()."' ORDER BY `courseattendence`.`ATTENDANCE_SEMESTER` ASC";
			$query = mysql_query($sql);  
			while($row=mysql_fetch_array($query)) {
				$temp = new Attendance;
				$temp->setAll($row["ATTENDANCE_ID"],$row["STUDENT_ID"],$row["COURSE_ID"],$row["FINAL_GRADE"],$row["ATTENDANCE_SEMESTER"]);
				$this->courseAttendence[] = $temp;
			}
			
		}
		
		return $this->courseAttendence;
	}
	
	function getCourseRegistrationID() {		
		$this->courseRegistrationID = array();
		$sql = "SELECT * FROM `courseregistration` WHERE student_id like '". $this->getStudentId()."'";
		$query = mysql_query($sql);  
		while($row=mysql_fetch_array($query)) {
			$this->courseRegistrationID[] = $row["COURSE_ID"];
		}
		
		return $this->courseRegistrationID;
	}
	
	/**
	 * Hàm dùng kiểm tra môn học có dk dc hay ko
	 * return:   0: OK 
	 *			>0: ID môn học cần đăng ký chung - PARALLEL (3), giả sử như đã đọc trước môn cần PARALLEL thì cũng ko dc
	 *			<0:	ID môn học cần học/đậu trước - PRECEDING (1) / PREREQUISITE (2)
	 * NOTE: Khi có nhiều dk cùng một lúc thì phải thỏa 2 điều kiện PRECEDING / PREREQUISITE trước khi dc xét. Và do kiểu xếp ASC, dk PRECEDING / PREREQUISITE luôn dc xét trước nếu có
	 */
	function checkCourseAvailability($courseID) {
		//Tao danh sach id cac mon da hoc
		
		//Check bang Relationship trong database
		$sql = "SELECT * FROM `courserelationship` WHERE `COURSE_ID` LIKE '".$courseID."' ORDER BY `courserelationship`.`COURSE_REL_TYPE` ASC  ";
		$query = mysql_query($sql);  
		$result = 0;
		while($row=mysql_fetch_array($query)) {
			$secondCourseID = $row["SECOND_COURSE_ID"];
			
			$grade = $this->lookupFinalGrade($secondCourseID);
			//return $grade;////////////////////////////////////////////////////
			switch (strtoupper($row["COURSE_REL_TYPE"])) {
				case "1": //Co hoc la dc
					if ($grade < 0) {
						return $secondCourseID *(-1);
					}
					break;
				case "2": //Co hoc va dau
					if ($grade < 5.5) {
						return $secondCourseID *(-1);
					}
					break;
				case "3": //Hoc song song
					return $secondCourseID ;
					break;
				case "4": // Khong co lien ket
					return 0;
					break;				
			}
		}
		return $result;
	}
	
	function lookupFinalGrade($courseID) {
		foreach ($this->getAttendance() as $att) {
			if($att->getCourseId() == $courseID) {
			 	return $att->getFinalGrade();
			}
		}
		return -1;
	}
	
}

?>